package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.PageResult;
import com.itheima.constant.QueryPageBean;
import com.itheima.dao.RoleDao;
import com.itheima.domain.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/10/15 11:20
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    //查询全部
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    //分页查询;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> roles = roleDao.findPage(queryString);
        long total = roles.getTotal();
        List<Role> rows = roles.getResult();
        return new PageResult(total, rows);
    }

    //添加数据;
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //操作t_role表;
        roleDao.add(role);
        Integer roleId = role.getId();
        //操作t_role_permission表 ;
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap();
                map.put("role_id", roleId);
                map.put("permission_id", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
        //操作t_role_menu表;
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map map = new HashMap();
                map.put("role_id", roleId);
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    //删除数据;
    @Override
    public void deleteByid(Integer id) {
        roleDao.deleteAssocication(id);
        roleDao.deleteAssocicationMenu(id);
        roleDao.deleteByid(id);
    }

    //根据ID查询,用于页面数据回显;
    @Override
    public Role findByid(Integer id) {
        return roleDao.findByid(id);
    }

    //角色关联的权限;
    @Override
    public List<Integer> getPermissionIds(Integer id) {
        return roleDao.getPermissionIds(id);
    }

    //角色关联的页面;
    @Override
    public List<Integer> getMenuIds(Integer id) {
        return roleDao.getMenuIds(id);
    }

    //修改数据;
    @Override
    public void update(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //操作role表中数据;
        roleDao.update(role);
        Integer roleId = role.getId();
        //清除之前的关系;
        //操作t_role_permission表
        roleDao.deleteAssocication(roleId);
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap();
                map.put("role_id", roleId);
                map.put("permission_id", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
        //清除之前的关系;
        //操作t_role_menu表
        roleDao.deleteAssocicationMenu(roleId);
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map map = new HashMap();
                map.put("role_id", roleId);
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
}
